﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FrankensteamClient
{
    /// <summary>
    /// Interaction logic for PrivateChatContent.xaml
    /// </summary>
    public partial class PrivateChatContent : UserControl
    {
        string _otherUser;

        public PrivateChatContent()
        {
            this.InitializeComponent();
            ClientAPI.Model.Instance._updateChatHandler += UpdateChatPage;

            ClientAPI.Model.Instance.ChatMessageReceived();
            ClientAPI.Model.Instance.UpdateChatList();
        }

        public void SetOtherUser(string otherUser)
        {
            _otherUser = otherUser;
        }

        private void UpdateChatPage(object sender, UpdateChatEventArgs eargs)
        {
            if (!lstChatRoom.Dispatcher.CheckAccess())
            {
                lstChatRoom.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                {
                    lstChatRoom.Text = ClientAPI.Model.Instance.GetChatLog(_otherUser);
                })
                );
            }
            else
                lstChatRoom.Text = ClientAPI.Model.Instance.GetChatLog(_otherUser);
        }

        private void btnSendChat_Click(object sender, RoutedEventArgs e)
        {
            if (ClientAPI.Model.Instance.IsConnected())
            {
                if (txtChat.Text != "" || txtChat.Text != null)
                {
                    ClientAPI.Model.Instance.SendChatMessage(_otherUser, txtChat.Text);
                }
                txtChat.Text = "";
            }
            else
                MessageBox.Show("warning not connected", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}
